/*
 * Main.java
 *
 * Created on 12 novembre 2007, 13:20
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */


// http://java.sun.com/docs/books/tutorial/uiswing/components/tabbedpane.html

package pika;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.* ;

public class VueVignettes extends JPanel {
    
    int xApercu;
    int yApercu;
    JList maListe;
    Fichier[] monTab;
    Test leModel;
    
    
    public VueVignettes(Test leModel, Fichier[] monTab, int xApercu, int yApercu) {
        this.leModel = leModel;
        this.monTab = monTab;
        this.xApercu = xApercu;
        this.yApercu = yApercu;
             
        setLayout(new BorderLayout());
    }
    
    
    
    
    public void creerVignette(){

        VignettesListModel monModel = new VignettesListModel(monTab); 

        VignetteListCellRenderer monRenderer = new VignetteListCellRenderer(monTab, xApercu, yApercu);
        maListe = new JList(monModel);
        maListe.setCellRenderer(monRenderer);
        maListe.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        maListe.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        maListe.setVisibleRowCount(-1);
        JScrollPane monScroll = new JScrollPane(maListe);
   //     monScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        this.add(monScroll,BorderLayout.CENTER);
        maListe.requestFocusInWindow();
        maListe.setDragEnabled(true);
   //     maListe.setPreferredSize(new Dimension(100,100));   bug ---> plus de scroll :/
        
        
        
        // gestion du double clic
        MouseListener monMouseListener = new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
                    int index = maListe.locationToIndex(e.getPoint());
                    int numeroRetour = VueVignettes.this.leModel.mesTabs.getSelectedIndex();
                    leModel.creerVueImage(monTab[index], index, VueVignettes.this.getWidth(), VueVignettes.this.getHeight(), numeroRetour);
                    
                }else if(SwingUtilities.isMiddleMouseButton(e)) { // middle click
                    int index = maListe.locationToIndex(e.getPoint());
                    int numeroRetour = VueVignettes.this.leModel.mesTabs.getSelectedIndex();
                  //  leModel.creerVueImageBackground(monTab[index],index, VueVignettes.this.getWidth(), VueVignettes.this.getHeight(), numeroRetour);
                    leModel.creerVueFlickr(monTab[index],numeroRetour);
                }
            }
        };
        maListe.addMouseListener(monMouseListener);
        
        
        KeyListener monKeyListener = new KeyListener() {
            
            public void  keyPressed(KeyEvent e) {
                int touche = e.getKeyCode();
                if (touche  == KeyEvent.VK_ENTER) {
                    int index[] = maListe.getSelectedIndices();
                    int premierPlan = 0;
                    int numeroRetour = VueVignettes.this.leModel.mesTabs.getSelectedIndex();
                    for (int i = 0; i < index.length; i++) {
                        if(premierPlan == 0) {
                            leModel.creerVueImage(monTab[index[i]], index[i], VueVignettes.this.getWidth(), VueVignettes.this.getHeight(), numeroRetour);
                            premierPlan = 1;
                        }else{
                            leModel.creerVueImageBackground(monTab[index[i]], index[i], VueVignettes.this.getWidth(), VueVignettes.this.getHeight(), numeroRetour);
                        }
                    }
                    
                    
                }
            }
            
            public void keyReleased(KeyEvent e){}
            public void keyTyped(KeyEvent e){}
        };
        
        maListe.addKeyListener(monKeyListener);
        
        
        Runnable vigne = new Runnable() {
            public void run() {
                
                for (int i = 0; i < monTab.length; i++) {
                    
                    monTab[i].creerVignette(xApercu, yApercu);
     //               maListe.firePropertyChange("test",0,1);
                    
                }
                
                
            }
        };
        
        SwingUtilities.invokeLater(vigne);
    }
    
}




class VignettesListModel extends DefaultListModel {
    Fichier[] monTab;
    
    public VignettesListModel(Fichier[] monTab) {
        this.monTab = monTab;
    }
    
    
    public Object getElementAt(int l){
        return monTab[l].getNom();
    }
    
    public int getSize() {
        return monTab.length;
    }
    
}



class VignetteListCellRenderer implements ListCellRenderer {
    
    JPanel panel;
    JPanel panel2;
    JPanel panel3;
    JPanel panel4;
    JLabel texte;
    JLabel image;
    
    Fichier[] monTab;
    
    public VignetteListCellRenderer(Fichier[] monTab, int xApercu, int yApercu) {
        this.monTab = monTab;
        
        
    }
    
    
    public Component getListCellRendererComponent(JList list,final Object value, int index, boolean isSelected, boolean cellHasFocus)  {
        list.requestFocusInWindow();
        panel = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        panel4 = new JPanel();
        texte = new JLabel();
        image = new JLabel();
        
        
        texte.setText((String)value);
        texte.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        panel.setToolTipText((String)value);
        
        panel.setLayout(new BorderLayout());
        panel2.setLayout(new BorderLayout());
        panel2.setPreferredSize(new Dimension(100,100));
        panel3.setLayout(new FlowLayout(FlowLayout.CENTER,1,1));
        
        
        if (value != null) {
            if(monTab[index]!=null) {
                image =  new JLabel(monTab[index].getVignette());
            }
        }
        
        panel2.add(image);
        
        panel3.add(texte);
        panel.setOpaque(true);
        panel.add(panel2,BorderLayout.NORTH);
        panel.add(panel3,BorderLayout.SOUTH);
        
        panel4.add(panel);
        
        texte.setOpaque(true);
        
        panel.setBackground(list.getBackground());
        panel2.setBackground(list.getBackground());
        panel3.setBackground(list.getBackground());
        panel4.setBackground(list.getBackground());
        
        
        if (isSelected) {
            panel2.setBorder(BorderFactory.createMatteBorder(3,3,3,3,list.getSelectionBackground()));
            texte.setForeground(Color.white);
            texte.setBackground(list.getSelectionBackground());
        } else {
            panel2.setBorder(BorderFactory.createLineBorder(new Color(236,236,230)));   // Window$ xp styyyyle
            texte.setBackground(list.getBackground());
        }
        
        
        
        panel4.setPreferredSize(new Dimension(129,143));    // Window$ xp styyyyle
        return panel4;
    }
    
    
}




